<template>
	<view>
		<view class="uni-common-mt">
			<view class="uni-form-item uni-column">
				<view class="title">商品名称</view>
				<input class="uni-input" v-model="FormData.name" focus placeholder="请输入商品名称" />
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">商品进价</view>
				<input class="uni-input" v-model="FormData.piceJj" placeholder="请输入商品进价" />
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">商品售价</view>
				<input class="uni-input" v-model="FormData.piceCs" placeholder="请输入商品售价" />
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">商品信息</view>
				<input class="uni-input" v-model="FormData.info" placeholder="请输入商品信息" />
			</view>
			<view class="uni-list">
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						请选择商品分类
					</view>
					<view class="uni-list-cell-db">
						<picker @change="bindPickerChange" :value="index" :range="array" range-key="name">
							<view class="uni-input">{{ array[index].name }}</view>
						</picker>
					</view>
				</view>
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">商品图片（链接、地址形式）</view>
				<input class="uni-input" v-model="FormData.image" placeholder="请输入商品图片（链接、地址形式）" />
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">库存</view>
				<input class="uni-input" v-model="FormData.stock" placeholder="请输入库存" />
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">规格</view>
				<input class="uni-input" v-model="FormData.size" placeholder="规格" />
			</view>
			<view class="uni-padding-wrap uni-common-mt">
				<button type="primary" @click="add">确认添加</button>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				array: [{name:'中国'}],
				index: 0,
				FormData: {
					name: uni.getStorageSync('stockGood').name,
					piceJj: uni.getStorageSync('stockGood').piceJj,
					piceCs: uni.getStorageSync('stockGood').piceCs,
					info: uni.getStorageSync('stockGood').info,
					image: uni.getStorageSync('stockGood').image,
					stock: uni.getStorageSync('stockGood').stock,
					size: uni.getStorageSync('stockGood').size,
					typeId: uni.getStorageSync('stockGood').typeId,
				},
			}
		},
		onLoad() {
			let params = {
				page: 1,
				limit: 99999,
			};
			this.init(params);
			console.log(uni.getStorageSync('stockGood').name)
		},
		methods: {
			init(params) {
				this.$api.selectGoodType(params).then((res) => {
					this.array = res.rows
					this.FormData.typeId = this.array[this.index].id
				}).catch((err) => {
					console.log('request fail', err);
				})
			},
			bindPickerChange(e) {
				console.log('picker发送选择改变，携带值为：' + e.target.value)
				this.index = e.target.value
				this.FormData.typeId = this.array[this.index].id
				console.log(this.FormData.typeId)
			},
			add() {
				console.log(this.FormData)
				let params = this.FormData
				params.classLevel = 1
				params.status = 1
				params.id = uni.getStorageSync('stockGood').id
				params.belongId = uni.getStorageSync('user').roleid
				/* for (let i in this.FormData) {
					console.log("姓名："+i+";"+"对应值："+this.FormData[i]);
					if (this.FormData[i] == '') {
						uni.showToast({
							icon: 'none',
							title: "请补全未填项",
							duration: 2000,
						});
						return false;
					}
				} */
				
				this.$api.addCommodity(params).then((res) => {
					if(res.success) {
						uni.showToast({
							icon: 'success',
							title: "添加成功",
							duration: 500,
						})
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/home/home'
							})
						},500)
					}else {
						uni.showToast({
							icon: 'none',
							title: res.msg,
							duration: 2000
						});
					}
				}).catch((err) => {
					console.log('request fail', err);
				})
			},
		},
	}
</script>

<style scoped>
	.title {
		padding: 10upx 25upx;
	}
	.uni-icon-clear,
	.uni-icon-eye {
		color: #999;
	}
</style>
